package org.hot100_0.leetcode239;

import java.util.ArrayDeque;
import java.util.Deque;

public class Solution {

    public static void main(String[] args) {
        int [] nums ={1,-1};
        int[] res = maxSlidingWindow(nums, 1);
        for (int num :res) {
            System.out.println(num);
        }
    }
    public static int[] maxSlidingWindow(int[] nums, int k) {

        int n = nums.length;
        int size = n - k + 1;
        int[] res = new int[size];

        int fast = 0;
        Deque<Integer> deque = new ArrayDeque<>();
        int i = 0;
        while (fast < n) {

            while ( !deque.isEmpty() && deque.peek()<fast-k+1){
                deque.pollFirst();
            }

            while (!deque.isEmpty() && nums[deque.peekLast()]<nums[fast]){
                deque.pollLast();
            }

            deque.add(fast);

            if (fast>=k-1){
                res[i]=nums[deque.peekFirst()];
                i++;
            }
            fast++;
        }

        return res;
    }
}
